/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Jul 24, 2008

package com.sceneworker.editors.distancemodeleditor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.DistanceSwitchModel;
import com.jme.scene.lod.DiscreteLodNode;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class DistanceModelEditorPanel extends JPanel {

	private		JButton										mcl_done;
	private		JDialog										mcl_par;
	private		DiscreteLodNode								mcl_distanceNode;
	private		DistanceSwitchModel							mcl_switchModel;
	private		JList										mcl_list = new JList();
	
	public	DistanceModelEditorPanel(JDialog cl_par, DiscreteLodNode cl_programObj) {
		mcl_par = cl_par;
		mcl_distanceNode = cl_programObj;
		
		if(!(mcl_distanceNode.getSwitchModel() instanceof DistanceSwitchModel)) {
			SimpleAlert.doAlert("The switch model associated with this Discrete LOD Node is not a distance switch model and cannot be edited.", null);
			return;
		}
		
		mcl_switchModel = (DistanceSwitchModel)mcl_distanceNode.getSwitchModel();
		
		buildPanel(cl_programObj);
	}
	
	private	void
	buildPanel(final DiscreteLodNode str_location) {
		setLayout(new MigLayout("inset 5"));
		
		initialiseListPanel();
		initialiseButtonPanel();
		
		mcl_done = SwingComponentFactory.makeAButton("Done");
		mcl_done.addActionListener(new ActionListener() {
			public	void
			actionPerformed(ActionEvent cl_e) {
				mcl_par.setVisible(false);
			}
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, mcl_done);
	}
	
	private	void	initialiseListPanel() {
		JPanel			lcl_panel = new JPanel(new MigLayout());
		
		mcl_list = new JList();
		DefaultListModel		lcl_model = new DefaultListModel();

		int			li_numC = mcl_switchModel.getNumChildren();

		for(int li_index = 0; li_index < li_numC; li_index++) {
			lcl_model.addElement(mcl_distanceNode.getChild(li_index).getName());
		}
		
		mcl_list.setModel(lcl_model);
		
		JScrollPane		lcl_scrollPane = new JScrollPane();
		lcl_scrollPane.setViewportView(mcl_list);
		lcl_scrollPane.setMinimumSize(new Dimension(300, 200));
		lcl_panel.add(lcl_scrollPane, "width 100%, height 100%");
		
		add(lcl_panel, "width 50%, height 100%");
	}
	
	private	void	initialiseButtonPanel() {
		JPanel			lcl_panel = new JPanel(new MigLayout());
		
		JButton			lcl_add = SwingComponentFactory.makeAButton("Add");
		lcl_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GameTaskQueueManager.getManager().update(new Callable<Void>() {
					public Void call() throws Exception {
						if(mcl_distanceNode.getChildren() == null) {
							SimpleAlert.doAlert("Cannot add Level Of Detail as Node has no children.", null);
							return null;
						}
						
						int		li_numC = mcl_switchModel.getNumChildren();
						if(li_numC == mcl_distanceNode.getChildren().size()) {
							SimpleAlert.doAlert("Cannot add Level Of Detail as Node has no child for this new distance entry.", null);
							return null;
						}
						
						mcl_switchModel.setModelDistance(li_numC, 0, 100);
						
						li_numC = mcl_switchModel.getNumChildren();
						DefaultListModel		lcl_model = (DefaultListModel)mcl_list.getModel();
						lcl_model.addElement(mcl_distanceNode.getChild(li_numC - 1).getName());
						
						mcl_list.repaint();
						return null;
					}
				});
			}
		});
		
		lcl_panel.add(lcl_add, "wrap");
		
		final	JTextField		lcl_min = MigLayoutUtils.numberText(lcl_panel, "Min", 0f, true);
		final	JTextField		lcl_max = MigLayoutUtils.numberText(lcl_panel, "Max", 0f, true);
		
		mcl_list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				int		li_selIndex = mcl_list.getSelectedIndex();
				
				if(li_selIndex < 0) {
					return;
				}
				
				float		lf_min = mcl_switchModel.getModelMinDistance(li_selIndex);
				float		lf_max = mcl_switchModel.getModelMaxDistance(li_selIndex);
				
				lcl_min.setText("" + lf_min);
				lcl_max.setText("" + lf_max);
			}
		});
		
		JButton			lcl_saveValues = SwingComponentFactory.makeAButton("Save Values");
		lcl_saveValues.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				GameTaskQueueManager.getManager().update(new Callable<Void>() {

					public Void call() throws Exception {
						int		li_selIndex = mcl_list.getSelectedIndex();
						
						if(li_selIndex < 0) {
							return null;
						}
						
						float		lf_min = Float.parseFloat(lcl_min.getText());
						float		lf_max = Float.parseFloat(lcl_max.getText());
						
						mcl_switchModel.setModelDistance(li_selIndex, lf_min, lf_max);
						return null;
					}
				});
			}
			
		});
		lcl_panel.add(lcl_saveValues);
		
		add(lcl_panel, "width 50%, wrap");
	}
	
	
}
